package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiU_chat_msg;

import cn.com.api.service.ApiU_chat_msgService;

import cn.com.util.*;


public class U_chat_msgBaseController {

	private static Logger logger = Logger.getLogger(U_chat_msgBaseController.class);
	
	@Autowired
	private ApiU_chat_msgService apiU_chat_msgService;
	
	/**
	 * 列表
	 * @return
	 */
	@RequestMapping(value = "u_chat_msg-list")
	public ModelAndView u_chat_msg_list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/u_chat_msg_list.jsp");
		
		List<ApiU_chat_msg> u_chat_msgs = apiU_chat_msgService.findU_chat_msgs();
		mv.addObject("u_chat_msgs", u_chat_msgs);
		
		return mv;
	}
	
	/**
	 * 进入新增或者编辑页面
	 * @return
	 */
	@RequestMapping(value = "u_chat_msg-add-pre")
	public ModelAndView u_chat_msg_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/u_chat_msg_add.jsp");
		//
		if(request.getParameter("id")!=null){
			String id = request.getParameter("id");
			ApiU_chat_msg u_chat_msg = apiU_chat_msgService.findU_chat_msgById(Integer.valueOf(id));
			mv.addObject("u_chat_msg",u_chat_msg);
		}
		return mv;
	}

	/**
	 *新增或者编辑
	 * @return
	 */
	@RequestMapping(value = "u_chat_msg-add")
	public void u_chat_msg_add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String id = request.getParameter("id");
		String from_uid = request.getParameter("from_uid");
		String to_uid = request.getParameter("to_uid");
		String add_time = request.getParameter("add_time");
		String is_deal = request.getParameter("is_deal");
		String type = request.getParameter("type");
		String content = request.getParameter("content");
		
		Map<String,Object> map = new HashMap<String,Object>();
		//String id = request.getParameter("id");
		//add
		if(id==null || (id.trim()).equals("")){
			ApiU_chat_msg u_chat_msg = new ApiU_chat_msg();
            /*
		    u_chat_msg.setId(id);
		    u_chat_msg.setFrom_uid(from_uid);
		    u_chat_msg.setTo_uid(to_uid);
		    u_chat_msg.setAdd_time(add_time);
		    u_chat_msg.setIs_deal(is_deal);
		    u_chat_msg.setType(type);
		    u_chat_msg.setContent(content);
	        */
			apiU_chat_msgService.insertU_chat_msg(u_chat_msg);
		}
		//edit
		else{
			ApiU_chat_msg u_chat_msg = apiU_chat_msgService.findU_chat_msgById(Integer.valueOf(id));
	        /*
		    u_chat_msg.setId(id);
		    u_chat_msg.setFrom_uid(from_uid);
		    u_chat_msg.setTo_uid(to_uid);
		    u_chat_msg.setAdd_time(add_time);
		    u_chat_msg.setIs_deal(is_deal);
		    u_chat_msg.setType(type);
		    u_chat_msg.setContent(content);
			*/
			apiU_chat_msgService.updateU_chat_msg(u_chat_msg);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
        JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	/**
	 *删除
	 * @return
	 */
	@RequestMapping(value = "u_chat_msg-delete")
	public void u_chat_msg_delete(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		
		//delete
		int ret_i =	apiU_chat_msgService.deleteU_chat_msg(Integer.valueOf(id));
		
		//
		if(ret_i > 0){
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
}



